import Vue from 'vue'
import App from './App.vue'
import {
    Button,
    Container,
    Header,
    Main,
    Aside,
    Menu,
    Submenu,
    MenuItem,
    MenuItemGroup,
    RadioGroup,
    RadioButton,
    Dropdown,
    DropdownMenu,
    DropdownItem,
    Card,
    Row,
    Col,
    Table,
    TableColumn,
    Breadcrumb,
    BreadcrumbItem,
    Tag,
    Form,
    FormItem,
    Input,
    Select,
    Option,
    Switch,
    DatePicker,
    Dialog, Pagination,
    MessageBox,
    Message,
    Loading
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

// 路径不加文件名则默认引入目录下的index文件
import router from '../router'
import store from '../store'
import http from 'axios'
import * as Common from '../utils/common'
import Cookies from 'js-cookie'

// import '../api/mock.js'
if (process.env.NODE_ENV === "development") {
    require('../api/mock.js')
}

Vue.config.productionTip = false
Vue.use(Button);
Vue.use(Container);
Vue.use(Header);
Vue.use(Main);
Vue.use(Aside);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(RadioGroup);
Vue.use(RadioButton);
Vue.use(Dropdown);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(Card)
Vue.use(Row)
Vue.use(Col)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Tag)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Select)
Vue.use(Option)
Vue.use(Switch)
Vue.use(DatePicker)
Vue.use(Dialog)
Vue.use(Pagination)
Vue.use(Loading.directive)

Vue.prototype.$http = http
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$message = Message
Vue.prototype.$common = Common
Vue.prototype.$cookie = Cookies

/**
 * 导航守卫
 */
router.beforeEach((to, from, next) => {
    // 调用全局函数getToken，目的是防止页面刷新后丢失token信息
    store.commit('getToken');
    const token = store.state.user.token;

    if (!token && to.name !== 'login') {
        // 如果token不存在并且要进入的路由不等于login，则跳转到login
        next({ name: 'login' });
    } else if (token && to.name === 'login') {
        // 如果是已登录状态，且要进入的login路由，则跳转到首页
        next( {name: 'home' })
    } else {
        // 继续
        next();
    }
})

new Vue({
    http,
    store,
    router,
    render: h => h(App),
}).$mount('#app')
